﻿#region License
/*Copyright (c) 2010 Andrew Collard (andrewcollard@hotmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#endregion

using System.IO;

namespace TorchView.Game
{
  public sealed class ActiveDungeon
  {
    public uint CurrentFloor { get; set; }
    public uint MapSeed { get; set; }
    public string Name { get; set; }
    public byte Unknown2 { get; set; }
    public byte Unknown3 { get; set; }
    public uint[][] Unknown4 { get; set; }
    public Character[] Characters { get; set; }
    public Item[] DungeonItems { get; set; }
    public Entity[] Entities { get; set; }
    public string[] Layouts { get; set; }
    public Formation[] Formations { get; set; }

    public static ActiveDungeon Read(BinaryReader reader)
    {
      ActiveDungeon dungeon = new ActiveDungeon();
      dungeon.CurrentFloor = reader.ReadUInt32();
      dungeon.MapSeed = reader.ReadUInt32();
      dungeon.Name = SVTHelper.ReadString(reader);
      dungeon.Unknown2 = reader.ReadByte();
      dungeon.Unknown3= reader.ReadByte();
      int aCount = reader.ReadInt32();
      int bCount = reader.ReadInt32();
      uint[][] unknown4 = new uint[aCount][];
      for (int a = 0; a < aCount; a++)
      {
        var unknowns = new uint[bCount];
        unknown4[a]=unknowns;
        for (int b = 0; b < bCount; b++)
          unknowns[b] = reader.ReadUInt32();
      }
      dungeon.Unknown4 = unknown4;
      ReadCharacters(dungeon, reader);
      ReadDungeonItems(dungeon, reader);
      ReadEntities(dungeon, reader);
      ReadLayouts(dungeon, reader);
      ReadFormations(dungeon, reader);

      return dungeon;
    }

    private static void ReadLayouts(ActiveDungeon dungeon, BinaryReader reader)
    {
      uint layoutCount = reader.ReadUInt32();
      string[] layouts = new string[layoutCount];
      for (int i = 0; i < layoutCount; i++)
        layouts[i] = SVTHelper.ReadString(reader);
      dungeon.Layouts = layouts;
    }

    private static void ReadFormations(ActiveDungeon dungeon, BinaryReader reader)
    {
        uint formationCount = reader.ReadUInt32();
        Formation[] formations = new Formation[formationCount];
        for (int i = 0; i < formationCount; i++)
            formations[i] = Formation.Read(reader);
        dungeon.Formations = formations;
    }

      private static void ReadEntities(ActiveDungeon dungeon, BinaryReader reader)
    {
      uint entityCount = reader.ReadUInt32();
      Entity[] entities = new Entity[entityCount];
      for (int i = 0; i < entityCount; i++)
        entities[i] = Entity.Read(reader);
      dungeon.Entities = entities;
    }

    private static void ReadDungeonItems(ActiveDungeon dungeon, BinaryReader reader)
    {
      uint itemsCount = reader.ReadUInt32();
      Item[] items = new Item[itemsCount];
      for (int i = 0; i < itemsCount; i++)
        items[i] = Item.Read(reader);
      dungeon.DungeonItems = items;
    }

    private static void ReadCharacters(ActiveDungeon dungeon, BinaryReader reader)
    {
      uint characterCount = reader.ReadUInt32();
      Character[] characters = new Character[characterCount];
      for (int i = 0; i < characterCount; i++)
        characters[i] = Character.Read(reader);
      dungeon.Characters = characters;
    }

    public override string ToString()
    {
      return "Active Dungeon: "+ Name;
    }
  }
}
